using System;
using System.Web.Mvc;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using rmeloni.Models;

namespace rmeloni.Helpers
{
    public class ThumbnailResult : ActionResult
    {
        public long ProjecId { get; set; }
        
        public ThumbnailResult(long prjid)
        {
            this.ProjecId = prjid;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            ProjectRepository prjRepo = new ProjectRepository();
            var projetoid = prjRepo.SelectById(ProjecId);

            BinaryFormatter bf = new BinaryFormatter();
            // copy the bytes to the memory
            MemoryStream ms = new MemoryStream(projetoid.Imagem);
            Bitmap imagem = (Bitmap)bf.Deserialize(ms);

            try
            {
                context.HttpContext.Response.ContentType = "image/gif";
                imagem.Save(context.HttpContext.Response.OutputStream, ImageFormat.Png);
                return;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                imagem.Dispose();
            }
            
        }
    }
}